package cn.chenhuanming.shh.authentication.server.security;

import cn.chenhuanming.utils.jwt.core.JWTSuccessHandler;
import cn.chenhuanming.utils.jwt.core.TokenUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by chenhuanming on 2017-08-15.
 *
 * @author chenhuanming
 */
@Component
public class SuccessHandler extends JWTSuccessHandler {

    public SuccessHandler(TokenUtils tokenUtils) {
        super(tokenUtils, new ObjectMapper());
    }

    @Override
    public Object getResult(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        return new Result(tokenUtils.generateToken(authentication),authentication);
    }

    @Data
    @AllArgsConstructor
    private class Result{
        private String token;
        private Authentication authentication;
    }
}
